﻿#pragma once

#include "pch.h"
#include "UdpClient.h"

struct UdpServer
{
	uint64_t sock;

	struct Desc
	{
		unsigned int port = 6666;
		unsigned int af = AF_INET;
		unsigned int type = SOCK_DGRAM;
		unsigned int protocol = IPPROTO_UDP;

		unsigned int sAddr = INADDR_ANY;
		unsigned int sinFamily = AF_INET;

		unsigned int backLog = 10;
	};

	bool Create(const Desc& desc);

	template<typename T>
	int RecvFrom(T& revbuf, UdpClient& client)
	{
		auto& soserver = client.clientAddr;
		static socklen_t addrlen = sizeof(struct sockaddr_in);

//      recvfrom(int fd, void* const buf __pass_object_size0, size_t len, int flags, struct sockaddr* src_addr, socklen_t* addr_len)


        return recvfrom(sock, (void*)&revbuf, sizeof(revbuf), 0, (sockaddr*)&soserver, &addrlen);
	}

	template<typename T>
	int SendTo(const T& senbuf, UdpClient& client)
	{
		auto& soserver = client.clientAddr;
		static int addrlen = sizeof(struct sockaddr_in);
        return sendto(sock, (const char*)&senbuf, sizeof(senbuf), 0, (sockaddr*)&soserver, addrlen);
	}

	void Close();
};

